package com.healthcloud.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.healthcloud.dao.PatientSNMapper;
import com.healthcloud.entity.InstrumentInfoEntity;
import com.healthcloud.entity.PatientSNEntity;
import com.healthcloud.exception.ReturnException;
import com.healthcloud.service.InstrumentInfoService;
import com.healthcloud.service.PatientSNService;
import com.healthcloud.utils.PageParam;

/**
 * 
 * @author zj
 * 2017年7月20日 下午4:43:57
 */
@Service
@Transactional
public class PatientSNServiceImpl implements PatientSNService {

	@Autowired
	private PatientSNMapper patientSNMapper;
	
	@Autowired
	private InstrumentInfoService instrumentInfoService;

	@Override
	public PatientSNEntity findBySn(String sn) {

		return null;
	}

	@Override
	public List<PatientSNEntity> findByEntityAll(PatientSNEntity entity) {
		return patientSNMapper.findByEntityAll(entity);
	}

	@Override
	public int save(PatientSNEntity entity) {
		return patientSNMapper.save(entity);
	}

	@Override
	public int update(PatientSNEntity entity) {
		return patientSNMapper.update(entity);
	}

	@Override
	public void updateBinding(PatientSNEntity patientSNEntity, PatientSNEntity oldPatientSNEntity) throws ReturnException {
		/**
		 * 解除绑定
		 */
		oldPatientSNEntity = relieve(oldPatientSNEntity);
		/**
		 * 绑定
		 * 赋值设备类型，设备型号
		 */
//		patientSNEntity.setType(oldPatientSNEntity.getType());
//		patientSNEntity.setModel(oldPatientSNEntity.getModel());
		binding(patientSNEntity);
	}

	@Override
	public void binding(PatientSNEntity patientSNEntity) throws ReturnException {
		/**
         * 设备是否已经绑定过了
         * 身份证号和sn设备号不能为空
         */
    	if (StringUtils.isEmpty(patientSNEntity.getIdCard())&&StringUtils.isEmpty(patientSNEntity.getSn())) {
			throw new ReturnException("身份证号和sn设备号不能为空");
		}
    	
    	PatientSNEntity entity = new PatientSNEntity();
    	entity.setSn(patientSNEntity.getSn());
    	entity.setStatus(0);
    	List<PatientSNEntity> entitys = findByEntityAll(entity);
    	if (entitys.size()>0) {
    		throw new ReturnException("设备已绑定");
		}

    	/**
    	 * 保存设备信息
    	 */
    	InstrumentInfoEntity info = new InstrumentInfoEntity();
    	info.setSn(patientSNEntity.getSn());
    	info.setModel(patientSNEntity.getModel());
    	info.setType(patientSNEntity.getType());
    	instrumentInfoService.add(info);
    	save(patientSNEntity);
	}

	@Override
	public PatientSNEntity relieve(PatientSNEntity patientSNEntity) throws ReturnException {
		/**
         * 身份证号和sn设备号不能为空
         */
    	if (StringUtils.isEmpty(patientSNEntity.getIdCard())&&StringUtils.isEmpty(patientSNEntity.getSn())) {
			throw new ReturnException("身份证号和sn设备号不能为空");
		}
    	
    	patientSNEntity.setStatus(0);
    	List<PatientSNEntity> entitys = findByEntityAll(patientSNEntity);
    	if (entitys.size()<=0) {
    		throw new ReturnException("没有已绑定记录");
		}
    	for (PatientSNEntity entity : entitys) {
    		patientSNEntity = entity;
    		entity.setStatus(1);
    		update(entity);
		}
    	
    	return patientSNEntity;
	}

	@Override
	public List<PatientSNEntity> search(PatientSNEntity entity,
			PageParam pageParam) {
		return patientSNMapper.search(entity, pageParam);
	}

	@Override
	public int count(PatientSNEntity entity) {
		return patientSNMapper.count(entity);
	}


}
